<?php

/**
 * 公共模块
 * @author Administrator
 */

namespace Partner\Controller;

use Partner\Model\Goods;
use Think\Controller;
use Think\IReq;
use Think\IFilter;
use Think\JSON;

class BlockController extends ComController {

    function goods_category() {
        $type = IReq::get('type') == 'checkbox' ? 'checkbox' : 'radio';
        $this->assign('type', $type);
        $this->display();
    }

    public function pic() {
        $specIndex = IReq::get('specIndex');
        $this->assign('specIndex', $specIndex);
        $this->display();
    }

    /**
     * @brief Ajax获取规格值
     */
    function spec_value_list() {
        // 获取POST数据
        $spec_id = IFilter::act(IReq::get('id'), 'int');

        //初始化spec商品模型规格表类对象
        $specObj = M('spec');
        //根据规格编号 获取规格详细信息
        $where = array(
            'id' => $spec_id
        );
        $specData = $specObj->where($where)->find();
        if ($specData) {
            echo JSON::encode($specData);
        } else {
            //返回失败标志
            echo '';
        }
    }

    /*
     * @breif ajax添加商品扩展属性
     * */

    function attribute_init() {
        $id = IFilter::act(IReq::get('model_id'), 'int');
        $tb_attribute = M('attribute');
        $attribute_info = $tb_attribute->where(array('model_id' => $id))->select();
        echo JSON::encode($attribute_info);
    }

    /**
     * @brief 获取地区
     */
    public function area_child() {
        $parent_id = intval(IReq::get("aid"));
        $areaDB = M('areas');
        $data = $areaDB->where(array('parent_id' => $parent_id))->order('sort asc')->select();
        echo JSON::encode($data);
    }

    /**
     * @brief 统计局分类
     */
    public function cat_child() {
        $parent_id = intval(IReq::get("aid"));
        $areaDB = M('product_cat');
        $data = $areaDB->where(array('parent_id' => $parent_id))->order('cat_id asc')->select();
        echo JSON::encode($data);
    }

    /*
     * 选择商品
     */

    public function search_goods() {
        $isProducts = IFilter::act(IReq::get('is_products'), 'int');
        $tmpType = IFilter::act(IReq::get('type'));
        $seller_id = IFilter::act(IReq::get('seller_id'), 'int');

        $this->assign('isProducts', $isProducts);
        $this->assign('tmpType', $tmpType);
        $this->assign('seller_id', $seller_id);
        $this->display();
    }

    //列出筛选商品
    function goods_list() {
        //商品检索条件
        $show_num = IFilter::act(IReq::get('show_num'), 'int');
        $keywords = IFilter::act(IReq::get('keywords'));
        $cat_id = IFilter::act(IReq::get('category_id'), 'int');
        $goods_no = IFilter::act(IReq::get('goods_no'));
        $is_products = IFilter::act(IReq::get('is_products'), 'int');
        $seller_id = $this->Seller['seller_id'];
        $goods_id = IFilter::act(IReq::get('goods_id'), 'int');

        //货号处理 商品货号或者货品货号2种情况
        if ($goods_no) {
            $productDB = M('products');
            $productData = $productDB->where(array('products_no' => $goods_no))->select();
            if ($productData) {
                foreach ($productData as $key => $item) {
                    $goods_id .= "," . $item['goods_id'];
                }
                //找到货品后清空货号数据
                $goods_no = "";
            }
        }

        //查询条件
        $table_name = 'goods as go';
        $fields = 'go.id as goods_id,go.name,go.img,go.goods_no,go.sell_price,go.spec_array';

        $where = array(
            'go.is_del' => 0
        );
        if ($seller_id) {
            $where['go.seller_id'] = $seller_id;
        }
        if ($goods_id) {
            $where[] = 'go.id in (' . $goods_id . ')';
        }
        if ($goods_no) {
            $where['goods_no'] = $goods_no;
        }
        if ($keywords) {
            $where[] = 'go.name like "%' . $keywords . '%"';
        }

        //分类筛选
        if ($cat_id) {
            $where['ca.category_id'] = $cat_id;
            $join = 'sm_category_extend as ca on go.id = ca.goods_id';
        }

        //获取商品数据
        $goodsDB = M($table_name);
        $data = $goodsDB->join($join)->where($where)->field($fields)->limit($show_num)->order('go.id desc')->select();

        //包含货品信息
        if ($is_products) {
            if ($data) {
                $goodsIdArray = array();
                foreach ($data as $key => $val) {
                    //有规格有货品
                    if ($val['spec_array']) {
                        $goodsIdArray[$key] = $val['goods_id'];
                        unset($data[$key]);
                    }
                }

                if ($goodsIdArray) {
                    $productFields = "pro.goods_id,go.name,go.img,pro.id as product_id,pro.products_no as goods_no,pro.spec_array,pro.sell_price,pro.store_nums";
                    $productDB = M('goods as go');
                    $productJoin = 'products as pro on go.id = pro.goods_id';
                    $productWhere = array(
                        "pro.goods_id in(' . join(',', $goodsIdArray) . ')'"
                    );
                    $productDate = $productDB->where($productWhere)->join($productJoin)->field($productFields)->select();
                    $data = array_merge($data, $productDate);
                }
            }
        }
        $codeObj = M("code");
        foreach ($data as $key => $value) {
            $code = $codeObj->where(array('good_id' => $value['goods_id']))->field('max(id) as max, min(id) as min')->find();
            $value['max'] = $code['max'];
            $value['min'] = $code['min'];
            $data[$key]['json'] = JSON::encode($value);
            if($value['spec_array']){
                $spec = JSON::decode($value['spec_array']);
                foreach ($spec as $v) {
                    $data[$key]['spec'][] = $v;
                }
            }
        }

        //dump($data);die;
        $this->assign('data', $data);
        $this->assign('type', IFilter::act(IReq::get('type'))); //页面input的type类型，比如radio，checkbox
        $this->display();
    }

}